#!/usr/local/bin/bash

# GROUP is the distributed shell (dsh) group name that specifies which machines
# to connect to to run the command. There are several files with lists of varying
# size.

# Note: There are no guarantees that the machines listed are up or if they still
# exist. These lists may be out of date or machines could be powered off. Please
# use with care and update if necessary (may involve contacting the IT department).

# GROUP can be 50, 100, all, etc...

DELAY=3

GROUP=$1
TIME=`date -d"${DELAY}minutes" | awk '{print $4}' | sed -e 's/:..$//'`
COMMANDLINE=$2
CHILDREN=10
ATINSTALL="/usr/local/devel/ANNOTATION/ANTware/atjob.pl --time $TIME --children $CHILDREN --command \"$COMMANDLINE\""
DSH=$HOME/bin/dsh
DSH_OPTIONS="-c -r ssh"

GROUP_FILE=$HOME/.dsh/group/$GROUP
MACHINE_COUNT=`cat $GROUP_FILE | wc -l`
JOB_COUNT=`echo "scale=3; $CHILDREN*$MACHINE_COUNT" | bc | awk -F '.' '{print $1}'` 

if [ -f $GROUP_FILE ]; then
    $DSH $DSH_OPTIONS --remoteshellopt "-o CheckHostIP=no" --remoteshellopt "-o StrictHostKeyChecking=no" -g $GROUP -- $ATINSTALL 
    echo "$JOB_COUNT jobs from $MACHINE_COUNT machines will be fired off at $TIME."
else
   echo "No such group $GROUP"
   exit 255
fi
